package com.casstime.ec.cloud.meeting.web.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * <p>
 * Title: DateUtils
 * </p>
 * <p>
 * Description: 获取日期时间段工具类
 * </p>
 * 
 * @author Harry Chang
 * @Email hailong.chang@casstime.com
 * @Company CassTime Technologies Co.Ltd.
 * @date 2017-05-09 16:03:07
 */
public class DateSectionUtils {

	private static final Logger logger = LoggerFactory.getLogger(DateSectionUtils.class);

	public enum DateSection {

		/**
		 * 最近3个月
		 */
		RECENTLY_THREE_MONTH(1),
		/**
		 * 今年内
		 */
		WITHIN_THIS_YEAR(2),
		/**
		 * 上一年
		 */
		LAST_YEAR(3),
		/**
		 * 最近一周
		 */
		RECENTLY_ONE_WEEK(4),
		/**
		 * 最近一个月
		 */
		RECENTLY_ONE_MONTH(5);

		private Integer value;

		/**
		 * 默认返回3个月
		 * @param value 1：最近3个月;2:今年内;3:去年;4:最近一周;5:最近一个月;其它：最近3 个月
		 * @return
		 */
		public static DateSection valueOf(int value){
			switch(value){
				case 1: return RECENTLY_THREE_MONTH;
				case 2: return WITHIN_THIS_YEAR;
				case 3: return LAST_YEAR;
				case 4: return RECENTLY_ONE_WEEK;
				case 5: return RECENTLY_ONE_MONTH;
				default: return RECENTLY_THREE_MONTH;
			}
		}
		
		DateSection(int value) {
			this.value = value;
		}

		public Integer getValue() {
			return value;
		}

	}

	private DateSectionUtils() {

	}

	public static class Result {

		private Date beginDate;

		private Date endDate;

		private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		public Result(Date beginDate, Date endDate) {
			this.beginDate = beginDate;
			this.endDate = endDate;
		}

		public Date getBeginDate() {
			return beginDate;
		}

		public Date getEndDate() {
			return endDate;
		}

		public String getBeginDateFormat() {
			return beginDate != null ? sdf.format(beginDate) : "";
		}

		public String getEndDateFormat() {
			return endDate != null ? sdf.format(endDate) : "";
		}
	}

	public static final Result getSectionDate(DateSection dateSection) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		switch (dateSection) {
			case RECENTLY_THREE_MONTH: {
				calendar.add(Calendar.MONTH, -3);
				calendar.add(Calendar.DATE, 1);
				return new Result(calendar.getTime(), new Date());
			}
			case WITHIN_THIS_YEAR: {
				Date begin = null;
				try {
					begin = sdf.parse(calendar.get(Calendar.YEAR) + "-01-01 00:00:00");
				} catch (ParseException e) {
					logger.error("格式化时间异常", e);
				}
				return new Result(begin, new Date());
			}
			case LAST_YEAR: {
				Date begin = null;
				Date end = null;
				try {
					begin = sdf.parse((calendar.get(Calendar.YEAR) - 1) + "-01-01 00:00:00");
					end = sdf.parse((calendar.get(Calendar.YEAR) - 1) + "-12-31 23:59:59");
				} catch (ParseException e) {
					logger.error("格式化时间异常", e);
				}
				return new Result(begin, end);
			}
			case RECENTLY_ONE_WEEK: {
				calendar.add(Calendar.DATE, -7);
				return new Result(calendar.getTime(), new Date());
			}
			case RECENTLY_ONE_MONTH: {
				calendar.add(Calendar.MONTH, -1);
				calendar.add(Calendar.DATE, 1);
				return new Result(calendar.getTime(), new Date());
			}
			default: {
				return new Result(new Date(), new Date());
			}
		}
	}

	public static void main(String[] args) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Result recentlyThreeMonth = DateSectionUtils.getSectionDate(DateSection.RECENTLY_THREE_MONTH);

		logger.info("begin:{}, end:{}", sdf.format(recentlyThreeMonth.getBeginDate()),
				sdf.format(recentlyThreeMonth.getEndDate()));

		Result withinThisYear = DateSectionUtils.getSectionDate(DateSection.WITHIN_THIS_YEAR);
		logger.info("begin:{}, end:{}", sdf.format(withinThisYear.getBeginDate()),
				sdf.format(withinThisYear.getEndDate()));

		Result lastYear = DateSectionUtils.getSectionDate(DateSection.LAST_YEAR);
		logger.info("begin:{}, end:{}", sdf.format(lastYear.getBeginDate()), sdf.format(lastYear.getEndDate()));

		Result recentlyOneWeek = DateSectionUtils.getSectionDate(DateSection.RECENTLY_ONE_WEEK);
		logger.info("begin:{}, end:{}", sdf.format(recentlyOneWeek.getBeginDate()),
				sdf.format(recentlyOneWeek.getEndDate()));

		Result recentlyOneMonth = DateSectionUtils.getSectionDate(DateSection.RECENTLY_ONE_MONTH);
		logger.info("begin:{}, end:{}", sdf.format(recentlyOneMonth.getBeginDate()),
				sdf.format(recentlyOneMonth.getEndDate()));
	}
}
